#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // do not allow resizing
    this->setFixedSize(this->width(), this->height());
}

MainWindow::~MainWindow()
{
    delete ui;

}

void
MainWindow::on_actionStart_Capture_triggered()
{
    if (!mCapture.isOpened()) {

        if (!mCapture.open (0)) {
            return;
        }

        startTimer(50);

    }
}

void
MainWindow::timerEvent(QTimerEvent *event)
{
    cv::Mat image;

    mCapture >> image;

    ui->openCVviewer->showImage(image);
}


void MainWindow::on_actionStop_Capture_triggered()
{
    mCapture.release();
}

void MainWindow::on_pushButton_clicked()
{
    this->on_actionStart_Capture_triggered();
}

void MainWindow::on_pushButton_2_clicked()
{
    this->on_actionStop_Capture_triggered();
}

void MainWindow::on_pushButton_3_clicked()
{
    QString imagePath = QFileDialog::getOpenFileName( this, "Select the QRCode file You Want to read!",
                                                        "", "Images (*.png *.xpm *.jpg)");

    if (!imagePath.isEmpty()) {

        cv::Mat image = cv::imread(imagePath.toStdString());

        ui->openCVviewer->showImage(image);

    }


}
